#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from informix.comm.const import AllStepEnum
from informix.job_manager import JobManager
from informix.param.backup_params_parse import InformixParseBackupParam
from informix.service.BaseService import MetaService
from informix.service.backup.backup_task import BackupTask


class BackupService(MetaService):
    def __init__(self, job_manager: JobManager, param_dict):
        super().__init__(job_manager, param_dict)
        self.param = self.gen_param_obj()
        self.job_manager = job_manager
        self.backup_manager = BackupTask(self.job_manager, self.param)
    
    def gen_param_obj(self):
        param = InformixParseBackupParam(self.param_dict, self.job_manager)
        return param
    
    def get_steps(self):
        pass


@JobManager.register(AllStepEnum.ALLOW_BACKUP_IN_LOCAL_NODE)
class AllowBackupService(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False
    
    def get_steps(self):
        """
        """
        default_allow = [
            self.backup_manager.init_param,
            self.backup_manager.backup_allow
        ]
        return default_allow


@JobManager.register(AllStepEnum.CHECK_BACKUP_JOB_TYPE)
class CheckBackupType(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False
    
    def get_steps(self):
        steps = [
            self.backup_manager.init_param,
            self.backup_manager.check_backup_job_type,
        ]
        return steps


@JobManager.register(AllStepEnum.PRE_TASK)
class PrerequisiteBackupService(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True
    
    def get_steps(self):
        steps = [
            self.backup_manager.init_param,
            self.backup_manager.backup_pre_task,
        ]
        return steps


@JobManager.register(AllStepEnum.BACKUP_GEN_SUB)
class BackupGenSubJobService(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False
    
    def get_steps(self):
        steps = [
            self.backup_manager.init_param,
            self.backup_manager.gen_sub_job,
        ]
        return steps


@JobManager.register(AllStepEnum.BACKUP)
class BackupExecuteService(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True
    
    def get_steps(self):
        steps = [
            self.backup_manager.init_param,
            self.backup_manager.backup
        ]
        return steps


@JobManager.register(AllStepEnum.QUERY_SCAN_REPOSITORIES)
class QueryScanRepositoriesService(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False
    
    def get_steps(self):
        steps = [
            self.backup_manager.init_param,
            self.backup_manager.query_scan_repositories
        ]
        return steps


@JobManager.register(AllStepEnum.POST_TASK)
class PostBackupService(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True
    
    def get_steps(self):
        steps = [
            self.backup_manager.init_param,
            self.backup_manager.backup_post_job
        ]
        return steps


@JobManager.register(AllStepEnum.STOP_TASK)
class AbortJob(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False
    
    def get_steps(self):
        steps = [
            self.backup_manager.init_param,
            self.backup_manager.backup_abort_job
        ]
        return steps
